﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;
using System.Text;
using Earworm.Web.DBM;

namespace Earworm.Web.Registration
{
    public partial class ForgotPassword : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        private static Random random = new Random((int)DateTime.Now.Ticks);//thanks to McAden 
        private string RandomString(int size)     
        {         
            StringBuilder builder = new StringBuilder();         
            char ch;         
            for (int i = 0; i < size; i++)         
            {             
                ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));                              
                builder.Append(ch);         
            }          
            return builder.ToString();     
        }  



        protected void btnEmail_Click(object sender, EventArgs e)
        {
            EarwormDataContext ear = new EarwormDataContext();

            var users = from u in ear.Users
                        where u.Email == txtEmail.Text
                        select u;

            if (users.Count() != 0)
            {



                string Rand1 = RandomString(8);

                User user = ear.Users.Single(u => u.Email == txtEmail.Text);
                user.TempPassword = Rand1;
                ear.SubmitChanges();


                MailHelper.SendMailMessage("littlevalder@gmail.com", txtEmail.Text, "", "", "Forgot Password", "Your Temporary Password is: " + Rand1 + "/n" + "http://localhost:50521/Registration/ResetPass.aspx");

            }

            else
            {
                CustomValidator val = new CustomValidator();
                val.IsValid = false;
                val.ErrorMessage = "Not a valid email";
                this.Page.Validators.Add(val);
            }
        }

        public class MailHelper
        {
            /// <summary>
            /// Sends an mail message
            /// </summary>
            /// <param name="from">Sender address</param>
            /// <param name="to">Recepient address</param>
            /// <param name="bcc">Bcc recepient</param>
            /// <param name="cc">Cc recepient</param>
            /// <param name="subject">Subject of mail message</param>
            /// <param name="body">Body of mail message</param>
            public static void SendMailMessage(string from, string to, string bcc, string cc, string subject, string body)
            {
                // Instantiate a new instance of MailMessage
                MailMessage mMailMessage = new MailMessage();

                // Set the sender address of the mail message
                mMailMessage.From = new MailAddress(from);
                // Set the recepient address of the mail message
                mMailMessage.To.Add(new MailAddress(to));

                // Check if the bcc value is null or an empty string
                if ((bcc != null) && (bcc != string.Empty))
                {
                    // Set the Bcc address of the mail message
                    mMailMessage.Bcc.Add(new MailAddress(bcc));
                }      // Check if the cc value is null or an empty value
                if ((cc != null) && (cc != string.Empty))
                {
                    // Set the CC address of the mail message
                    mMailMessage.CC.Add(new MailAddress(cc));
                }       // Set the subject of the mail message
                mMailMessage.Subject = subject;
                // Set the body of the mail message
                mMailMessage.Body = body;


                // Set the format of the mail message body as HTML
                mMailMessage.IsBodyHtml = true;
                // Set the priority of the mail message to normal
                mMailMessage.Priority = MailPriority.Normal;

                // Instantiate a new instance of SmtpClient
                SmtpClient mSmtpClient = new SmtpClient();
                System.Net.NetworkCredential myCredential = new System.Net.NetworkCredential("littlevalder@gmail.com", "");

                mSmtpClient.Host = "smtp.gmail.com";
                mSmtpClient.ServicePoint.MaxIdleTime = 1;
         
                mSmtpClient.Credentials = myCredential;
                

                mSmtpClient.EnableSsl = true;

                // Send the mail message
                mSmtpClient.Send(mMailMessage);
            }
        }


    }
}